<?php

/**
 * User Roles Model
 *
 */
class Application_Model_UserRoles implements Zend_Acl_Role_Interface 
{
    protected $_aclRoleId=null;
    
    public function __construct($roleId=null)
    {
        $this->setRoleId($roleId);
    }
    
    /**
     * ACL getRoleId interface function
     *
     * @return string
     */
    public function getRoleId()
    {
        if ($this->_aclRoleId==null) return self::getDefaultRole();
        
        return $this->_aclRoleId;
    }
    
    /**
     * Sets ACL Role ID if valid
     *
     * @param string $roleId
     */
    public function setRoleId($roleId)
    {
        $this->_aclRoleId=(string) self::isValidRole($roleId) ? $roleId : self::getDefaultRole();
    }

    /**
     * Returns an array with valid roles
     *
     * first one is default
     * second one is default role for users
     * last one is admin
     *    
     * @return array
     */
    static function getValidRoles()
    {
        return Zend_Registry::get('config')->roles->toArray();
    }
    
    /**
     * Returns default role ID
     *
     * @return string
     */
    static function getDefaultRole()
    {
        $roles=self::getValidRoles();
        
        return $roles[0];
    }
    
     /**
     * Returns default user role ID
     *
     * @return string
     */
    static function getUserRole()
    {
        $roles=self::getValidRoles();
        
        return $roles[1];
    }
    
    /**
     * Returns admin role ID
     *
     * @return string
     */
    static function getAdminRole()
    {
        $roles=self::getValidRoles();
        
        return $roles[count($roles)-1];
    }
    
     /**
     * Checks if given role is valid
     *
     * @param string $role
     * @return bool
     */
    static function isValidRole($role)
    {
        if (empty($role)) return false;
        
        $valid=self::getValidRoles();
        
        return in_array($role,$valid) ? true : false;
    }
    
    /**
     * Returns role's human name
     *
     * @param string $role
     * @return string
     */
    static function getRoleName($role)
    {
        return Zend_Registry::get('locale')->_('role:'.$role);
    }
    
    /**
     * Returns an associative array with role names
     *
     * @return array
     */
    static function getRoles()
    {
        $roles=array();
        
        $valid=self::getValidRoles();
        
        foreach ($valid as $role)
        {
            $roles[$role]=self::getRoleName($role);
        }
        
        return $roles;
    }
}

